<?php

/**
 * @apiVersion 1.0.0
 * @api {POST} /wechat/Login/index 001手机验证码登录OK
 * @apiDescription 手机验证码登录
 * @apiGroup Pub
 * @apiPermission 公众号
 * @apiParam {string} mobile 手机号码
 * @apiParam {string} code 验证码
 * @apiParamExample {json} 请求示例:
 * {
 *      "mobile": "15577778888",
 *      "code": "123893",
 * }
 * @apiSuccess  {Int} code 状态
 * @apiSuccess  {Object} data 元数据
 * @apiSuccess  {String} msg 错误说明
 * @apiSuccessExample {json} 响应示例:
{

}

 * //操作未成功提示信息
 * {
 *      "code":0,
 *      "msg" :"错误说明"
 * }
 */


/**
 * @apiVersion 1.0.0
 * @api {POST} /wechat/Login/logout 002注销OK
 * @apiDescription 注销
 * @apiGroup Pub
 * @apiPermission 公众号

 * @apiSuccess  {Int} code 状态
 * @apiSuccess  {Object} data 元数据
 * @apiSuccess  {String} msg 错误说明
 * @apiSuccessExample {json} 响应示例:
{

}

 * //操作未成功提示信息
 * {
 *      "code":0,
 *      "msg" :"错误说明"
 * }
 */

namespace app\wechat\controller;


use app\model\FreshAdminAuthGroupAccess;
use app\model\FreshAdminAuthRule;
use app\model\FreshAdminMenu;
use app\model\FreshAdminUser;
use app\model\FreshAdminUserData;
use app\util\ReturnCode;
use app\util\Tools;
use think\Cache;
use think\Db;

class Login extends Base {

    const MOBILE = ['15573179664', '13107210904', '18817020102'];

    /**
     * 用户登录
     */
    public function index() {
        $mobile = $this->request->post('mobile');
        $code = $this->request->post('code');

        if (!$mobile) {
            return $this->buildFailed(ReturnCode::LOGIN_ERROR, '缺少登录手机号!');
        }
        if (!$code) {
            return $this->buildFailed(ReturnCode::LOGIN_ERROR, '缺少验证码!');
        }

        //验证验证码
        if(!in_array($mobile, self::MOBILE)){
            $cache_code = Cache::get('verificationCode_' . $mobile);
            if($cache_code != $code)   return $this->buildFailed(ReturnCode::LOGIN_ERROR, '验证码不正确');
        }

        $userInfo = \app\model\User::get(['mobile' => $mobile]);
        if (isset($userInfo->userID)) { //已注册
            if ($userInfo['status']) {
                //更新用户登录数据
                $userInfo->loginTimes ++;
                $userInfo->clientIP = $this->request->ip(1);
                $userInfo->lastLoginTime = time();
                $userInfo->save();
            } else {
                return $this->buildFailed(ReturnCode::LOGIN_ERROR, '用户已被封禁，请联系管理员');
            }
        } else { //注册
            $domain = $_SERVER['HTTP_HOST'];
            $config = config('program.'.$domain);
            $appid = $config['AppID'];
            $platform = Db::name('platform')->where('appid', $appid)->find();
            $add = [
                'mobile' => $mobile,
                'addTime' => time(),
                'status' => 1,
                'platformID' => isset($platform['id'])?$platform['id']:1,
                'clientIP' => $this->request->ip(1),
                'lastLoginTime' => time(),
                'loginTimes' => 1,
                'statusCheck' => 0
            ];
            $pass_list = config('pass.key');
            foreach ($pass_list as $item){
                $add[$item] = random(8, 'all');
            }
            $insertUserID = Db::name('user')->insertGetId($add);
            if(!$insertUserID)    return $this->buildFailed(ReturnCode::LOGIN_ERROR, '注册失败');
            $userInfo = Db::name('user')->where('userID', $insertUserID)->find();
        }
        $apiAuth = md5(uniqid() . time());
        cache('Login_wechat:' . $apiAuth, json_encode($userInfo), config('apiAdmin.ONLINE_TIME'));
        cache('Login_wechat:' . $userInfo['userID'], $apiAuth, config('apiAdmin.ONLINE_TIME'));


        $return['userID'] = $userInfo['userID'];
        $return['nickname'] = $userInfo['nickname'];
        $return['apiAuth'] = $apiAuth;

        return $this->buildSuccess($return, '登录成功');
    }





    public function logout() {
        $ApiAuth = $this->request->header('ApiAuth');
        cache('Login_wechat:' . $ApiAuth, null);
        cache('Login_wechat:' . $this->userInfo['userID'], null);

        return $this->buildSuccess([], '登出成功');
    }

}
